package com.yuandengta.yomall.member.service.impl;

import com.yuandengta.common.exception.BizCode;
import com.yuandengta.yomall.member.dao.MemberLevelDao;
import com.yuandengta.yomall.member.entity.MemberLevelEntity;
import com.yuandengta.yomall.member.exception.MemberException;
import com.yuandengta.yomall.member.vo.MemberRegisterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandengta.common.utils.PageUtils;
import com.yuandengta.common.utils.Query;

import com.yuandengta.yomall.member.dao.MemberDao;
import com.yuandengta.yomall.member.entity.MemberEntity;
import com.yuandengta.yomall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 检查手机号唯一
     * @param mobile
     * @return
     */
    @Override
    public void checkMobileUnique(String mobile) {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", mobile));
        if (count > 0) {
            throw new MemberException(BizCode.MOBILE_EXIST_EXCEPTION.getMessage(), BizCode.MOBILE_EXIST_EXCEPTION.getCode());
        }
    }

    @Override
    public void checkUsernameUnique(String username) throws MemberException {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count > 0) {
            throw new MemberException(BizCode.USER_EXIST_EXCEPTION.getMessage(), BizCode.USER_EXIST_EXCEPTION.getCode());
        }
    }

    @Override
    public void register(MemberRegisterVO registerVO) throws MemberException {

        // 检查用户名和手机号唯一
        checkUsernameUnique(registerVO.getUsername());
        checkMobileUnique(registerVO.getMobile());
        // 设置用户名和手机号
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setUsername(registerVO.getUsername());
        memberEntity.setMobile(registerVO.getMobile());

        // 设置会员默认等级
        MemberLevelEntity memberLevelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(memberLevelEntity.getId());

        // 设置密码, 进行盐值加密处理
        // BCryptPasswordEncoder.encode() 加密：先生成盐值，根据盐值以及明文密码生成密文
        // BCryptPasswordEncoder.matches(登录密码，数据库存储密文) 密码匹配
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodePassword = passwordEncoder.encode(registerVO.getPassword());
        memberEntity.setPassword(encodePassword);

        // 保存
        save(memberEntity);
    }

}
